/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @file TestSocket.c
* 
* Function for testing the socket library code.
* 
* @author Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <socket.cpp>

/**
* @example TestSocket.c
*/

int main(int argc, char *argv[])
{
	
	if(argc<2)
	{
		puts("\nUSE:\nTestSocket <type>\n");
		puts("types:");
		puts("\tserver <ip> <port>");
		puts("\tclient <ip> <port> <cadena>\n");
		puts("\t  e.g.");
		puts("\t    TestSocket serveronlyone localhost 13000 => Waits on port 13000 a client tcp.");
                puts("\t    TestSocket servermultiple localhost 13000 => Waits on port 13000 a client tcp.");
		puts("\t    TestSocket client localhost 13000 \"a pair of words\" => Sends 'a pair of words' to a server tcp on localhost with port 13000 .\n");

		exit(EXIT_SUCCESS);
	}

	if(strcmp(argv[1],"serveronlyone")==0)
	{
		char* buffer=(char*)malloc(1024*sizeof(char));
		CMSocket algo(atoi(argv[3]),argv[2],1024);
		algo.serverTcp();
                int id_accept=0;
                id_accept=algo.serverAccept();
                while(true)
                {
                    algo.serverRead(buffer,id_accept);
                    printf("%s",buffer);
                    if(strcmp(buffer,"exit")==0)
                    {    exit(0);}
                    sprintf(buffer,"ok");
                    algo.serverWrite(buffer,id_accept);
                    bzero(buffer, sizeof(1024));
                }
		free(buffer);
		exit(EXIT_SUCCESS);
	}

	if(strcmp(argv[1],"servermultiple")==0)
	{
		char* buffer=(char*)malloc(1024*sizeof(char));
		CMSocket algo(atoi(argv[3]),argv[2],2);
		algo.serverTcp();
                int id_accept=0;
                while(id_accept=algo.serverAccept())
                {
                    algo.serverRead(buffer,id_accept);
                    printf("%s\n",buffer);
                    if(strcmp(buffer,"exit")==1)
                        exit(0);
                }
		free(buffer);
		exit(EXIT_SUCCESS);
	}

	if(strcmp(argv[1],"client")==0)
	{
            char* aux=(char*)malloc(1024*sizeof(char));
            //sprintf(aux,"ok");
            CMSocket algo(atoi(argv[3]),argv[2],1024);
            algo.clientTcp();
            //algo.clientWrite(aux);
            while(true)
            {
            //    usleep(100000);
                sprintf(aux,"%c%c",'\r','\n');
                algo.clientWrite(argv[4]);algo.clientWrite(aux);
                //algo.clientWrite(argv[4]);algo.clientWrite(aux);
                //algo.clientWrite(argv[4]);algo.clientWrite(aux);
                sprintf(aux,"exit");
                algo.clientWrite(aux);
                sprintf(aux,"%c%c",'\r','\n');
                algo.clientWrite(aux);
                //usleep(10000000);
                sprintf(aux,"o");
                while(strcmp(aux,"ok")!=0)
                {
                    algo.clientRead(aux);
                    printf("paso");
                }
                
            }
            free(aux);
	}
	
}
